﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Net;
using System.Net.Mail;

namespace SoftwareEngineering_Project.Accounts
{
    public partial class register : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            
            
            //Add Data to the employee table//////////////////
            //////////////////////////////////////////////////
            Employee employee = new Employee(Fname.Text, Lname.Text, Email.Text);
            if (employee.CheckifuserExists(Email.Text))
            {

                //Errormessage.Text = "User exists already";
                Errormessage.Text = "Employee exists already";
                Errormessage.Visible = true;
            }
            else
            {
                if (employee.AddtoDataBase())
                {


                    //Add Data to the user table//////////////////
                    //////////////////////////////////////////////////
                    Int32 employeeID = employee.getEmployeeID();//Get the ID of the last ID created 
                    int RoleID = 12;  //12 ids the role ID by defalt to create regular user.
                    //create the temporary password SALT
                    Random random = new Random();
                    int rndPassword = random.Next(0, 100000);

                    User user = new User(employeeID, RoleID, Email.Text, Password.Text, rndPassword.ToString(), "InValid");
                    if (!user.AddtoDataBase())
                    {
                        Errormessage.Text = "Error accessing the database";
                        Errormessage.Visible = true;
                    }
                    Errormessage.Visible = true;
                    Errormessage.Text = sendEmail(rndPassword.ToString());
                    Response.Redirect("ConfirmRegistration.aspx");
                   
                }
                else
                {
                    Errormessage.Text = "Error accessing the database";
                    Errormessage.Visible = true;
                }

            }
          
  
           }
        string sendEmail(String password)
        {
            //string path = "D:\\Test.txt";
            try
            {
                MailMessage mail = new MailMessage();
                mail.To.Add("ouassim.sadellah@gmail.com");
                mail.From = new MailAddress("e-schedule@gmail.com");
                mail.Subject = "Welcome to E-Schedule";

                string Body = "Hi, Here's your temporary access code to log in to the application:"
                    +"User Name " + Email.Text + " Your password is: " + Password.Text +" your access code is: " +  password +
                              " To complete registration please click on this link : http://e-schedule.azurewebsites.net/Accounts/ConfirmRegistration.aspx";
                mail.Body = Body;

                mail.IsBodyHtml = true;

                SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587);
                smtp.Host = "smtp.gmail.com"; //Or Your SMTP Server Address  
                smtp.Credentials = new System.Net.NetworkCredential
                     ("ouassim.sadellah@gmail.com", "SunDancer+1981");
                //Or your Smtp Email ID and Password  
                smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
                smtp.EnableSsl = true;

                smtp.Send(mail);
                return "Please check your email to complete registration";
            }
            catch (Exception ex)
            {
               return ex.ToString();
            }
        }

    }
}